package com.ht.common.core.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.ht.common.core.constant.SysConstant;

import cn.hutool.core.util.StrUtil;


public class MessageUtils {
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	public final static String errMsgType="TXT999";
	public final static String FUNCTION_CODE="08";
	private static SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmssSSS");
	private static SimpleDateFormat YMD=new SimpleDateFormat("yyyyMMddHHmmss");
    public MessageUtils() {
    }

    public static String messageJson(Object data) {
        return JSON.toJSONString(data);
    }
    
    
    /**
	 * 通过文件名获得报文类型
	 * @param filename
	 * @param pos
	 * @return
	 */
	public static String getMsgTypeByUnderline(String filename,int pos) {
		if(StringUtils.isBlank(filename)) {
			return errMsgType;
		}
		String [] array=filename.split(SysConstant.UNDERLINE);
		if(array.length==0) {
			return errMsgType;
		}
		if(pos>=array.length) {
			return errMsgType;
		}
		return array[pos];
	}
	/**
	 * 
	 * @param msgType
	 * @param senderId
	 * @param recvId
	 * @return
	 */
	public static String genMsgXmlFileName(String msgType,String id,String senderId,String recvId) {
		
		String fileName=StrUtil.format("{}_{}_{}_{}_{}.xml", msgType,senderId,recvId,id,YMD.format(new Date()));
		return fileName;
	}
	/**
	 * 
	 * @param msgType
	 * @param senderId
	 * @param recvId
	 * @return
	 */
	public static String genMsgJsonFileName(String msgType,String id,String senderId,String recvId) {
		String fileName=StrUtil.format("{}_{}_{}_{}_{}.json", msgType,senderId,recvId,id,YMD.format(new Date()));
		return fileName;
	}
	
	/**
	 * 
	 * @param msgType
	 * @param senderId
	 * @param recvId
	 * @return
	 */
	public static String genAttachFileName(String msgType,String id,String fileName) {
		String messageId=StrUtil.format("{}_{}_{}", msgType,id,fileName);
		return messageId;
	}
	/**
	 * 通过附件messageId获取文件路径
	 * @param messageId
	 * @return
	 */
	public static String genAttachPathByMessageId(String messageId) {
		if(StringUtils.isBlank(messageId)) {
			return null;
		}
		String names[]=messageId.split(SysConstant.UNDERLINE);
		if(StringUtils.isEmpty(names)) {
			return null;
		}
		if(names.length<3) {
			return messageId;
		}
		return names[2];
	}
    
}
